package net.mikoo.seals.api.model.use;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.common.enums.CensorStatusEnum;
import net.mikoo.seals.common.enums.UsedStatusEnum;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.Upfile;

public class ActDetailResponse extends BaseResponse {

	private ActDetail actDetail;

	public ActDetail getActDetail() {
		return actDetail;
	}

	public void setActDetail(ActDetail actDetail) {
		this.actDetail = actDetail;
	}
	
	public static ActDetailResponse valueOf(DevicesAct actModel) {
		ActDetailResponse result = new ActDetailResponse();
		if(actModel == null) {
			return result;
		}
		
		ActDetail actDetail = new ActDetail();
		
		actDetail.setDevicesActId(actModel.getDevicesActId());
		actDetail.setDevicesId(actModel.getDevicesId());
		actDetail.setTotalTimes(actModel.getTotalTimes());
		actDetail.setUseTimes(actModel.getUseTimes());
		actDetail.setCensorStatus(actModel.getCensorStatus());
		
		if(actModel.getCensorTime() != null) {
			actDetail.setCensorTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(actModel.getCensorTime()));
		}
		
		if(actModel.getCensorPerson() != null) {
			actDetail.setCensorPartyName(actModel.getCensorPerson().getName());
		}
		
		if(actModel.getCensorStatus() != null) {
			CensorStatusEnum status = CensorStatusEnum.getByCode(actModel.getCensorStatus());
			if(status != null) {
				actDetail.setCensorStatus(status.getCode());
				actDetail.setCensorStatusName(status.getMessage());
			}
		}
		
		if(actModel.getUsedStatus() != null) {
			UsedStatusEnum usedStatus = UsedStatusEnum.getByCode(actModel.getUsedStatus());
			if(usedStatus != null) {
				actDetail.setUseStatus(usedStatus.getCode());
				actDetail.setUseStatusName(usedStatus.getMessage());
			}
		}
		
		if(actModel.getApply() != null) {
			Apply apply = actModel.getApply();
			if(apply.getPerson() != null) {
				actDetail.setApplyPersonPortrait(apply.getPerson().getPortrait());
				actDetail.setApplyPersonName(apply.getPerson().getName());
				actDetail.setApplyPersonPosition(apply.getPerson().getPosition());
			}
			
			if(apply.getUpfiles() != null && apply.getUpfiles().size() > 0) {
				List<String> applyVouchers = new ArrayList<String>();
				for(Upfile upfile : apply.getUpfiles()) {
					if(upfile != null && StringUtils.isNotBlank(upfile.getFileUrl())) {
						applyVouchers.add(upfile.getFileUrl());
					}
				}
				actDetail.setApplyVouchers(applyVouchers);
			}
		}
		
		if(actModel.getDevices() != null) {
			actDetail.setDevicesName(actModel.getDevices().getDevicesCode());
		}
		
		if(actModel.getSeal() != null) {
			actDetail.setSealName(actModel.getSeal().getSealName());
			actDetail.setIsForcePhoto(actModel.getSeal().getIsForcedPhoto());
		}
		
		if(actModel.getUpfiles() != null && actModel.getUpfiles().size() > 0) {
			List<String> urlList = new ArrayList<String>();
			for(Upfile upfile : actModel.getUpfiles()) {
				if(upfile != null && StringUtils.isNotBlank(upfile.getFileUrl())) {
					urlList.add(upfile.getFileUrl());
				}
			}
			actDetail.setVouchers(urlList);
		}
		
		actDetail.setFailTimes(actModel.getFailTimes());
		
		result.setActDetail(actDetail);
		return result;
	}
	
}
